<?php 
defined('IN_TS') or die('Access Denied.');

switch($ts){
	//话题审核
	case "topicaudit":
	
		$topicid = intval($_POST['topicid']);
		
		$strTopic = $new['group']->find('group_topic',array(
			'topicid'=>$topicid,
		));
		
		if($strTopic['isaudit']==0){
			$new['group']->update('group_topic',array(
				'topicid'=>$topicid,
			),array(
				'isaudit'=>1,
			));
			
			echo 0;exit;
			
		}
		
		if($strTopic['isaudit']==1){
			$new['group']->update('group_topic',array(
				'topicid'=>$topicid,
			),array(
				'isaudit'=>0,
			));
			
			echo 1;exit;
			
		}
		
		break;
		
	//加入小组
	case "joingroup":
		
		$userid = intval($TS_USER['userid']);
	
		$groupid = intval($_POST['groupid']);
		
		$strGroup = $new['group']->find('group',array(
			'groupid'=>$groupid
		));
		
		if($userid==0 || $groupid==0 || $strGroup==''){
			getJson('请登录后再加入小组',1,2,tsUrl('user','login'));
		}
		
		//管理员可以加入任何小组
		if($TS_USER['isadmin'] != 1){
			
			//除管理员外其他用户都要经过这一关审核
			if($strGroup['joinway'] == 1) getJson('本小组禁止加入！');
			
			//处理申请加入,成员审核
			if($strGroup['joinway'] == 2){
				$new['group']->replace('group_user_isaudit',array(
					'userid'=>$userid,
					'groupid'=>$strGroup['groupid'],
				),array(
					'userid'=>$userid,
					'groupid'=>$strGroup['groupid'],
				));


				$arr = array('user'=>$TS_USER['username'],'group'=>$strGroup['groupname']);
				aac('wechat')->sendwechat(3,$strGroup['userid'],$arr,SITE_URL.'index.php?app=group&ac=edit&groupid='.$groupid.'&ts=useraudit');

				/*
				$msg_userid = '0';
				$msg_touserid = $strGroup['userid'];
				$msg_content = $TS_USER['username'].'申请加入你管理的小组'.$strGroup['groupname'].',请处理一下^_^ ';
				$msg_tourl = tsUrl('group','show',array('id'=>$groupid));
				aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);*/


				getJson('加入小组申请提交成功，请等待管理员审核后加入。',1,1);
				
			}
			
			//先统计用户有多少个小组了，50个封顶
			$userGroupNum = $new['group']->findCount('group_user',array('userid'=>$userid));
			
			if($userGroupNum >= $TS_APP['joinnum']) getJson('你加入的小组总数已经到达'.$TS_APP['joinnum'].'个，不能再加入小组！');
			
			$groupUserNum = $new['group']->findCount('group_user',array(
				'userid'=>$userid,
				'groupid'=>$groupid,
			));
			
			if($groupUserNum > 0) getJson('你已经加入小组！',1,1);
		
		}
		
		$new['group']->create('group_user',array(
			'userid'=>$userid,
			'groupid'=>$groupid,
			'addtime'=>time(),
		));

		if($strGroup['isplace']>0){

			$arr['groupids']=getGroupIds($TS_USER['userid']);

			aac('user')->update('user_info',array('userid'=>$TS_USER['userid']),$arr);
		}



		
		//更新
		$count_group = $new['group']->findCount('group_user',array(
			'userid'=>$userid,
		));
		$new['group']->update('user_info',array(
			'userid'=>$userid,
		),array(
			'count_group'=>$count_group,
		));
		
		//计算小组会员数
		$count_user = $new['group']->findCount('group_user',array(
			'groupid'=>$groupid,
		));
		
		//更新小组成员统计
		$new['group']->update('group',array(
			'groupid'=>$groupid,
		),array(
			'count_user'=>$count_user,
		));
		
		getJson('加入成功！',1,1);
		
		break;
		
	//退出小组
	case "exitgroup":
	
		$userid = intval($TS_USER['userid']);
		
		$groupid = intval($_POST['groupid']);
		
		//判断是否是组长，是组长不能退出小组
		$strGroup = $new['group']->find('group',array(
			'groupid'=>$groupid
		));
		
		if($userid==0 || $groupid==0 || $strGroup==''){
			getJson('非法操作');
		}
		
		if($strGroup['userid'] == $userid) getJson('组长任务艰巨，请坚持到底！');

		$groupuser = $new['group']->find('group_user',array('userid'=>$userid,'groupid'=>$groupid));

		if($groupuser['isstock']==0){
			$new['group']->delete('group_user',array(
				'userid'=>$userid,
				'groupid'=>$groupid,
			));

			if($groupuser['isvo']==1){

				aac('user')->update('user_info',array('userid'=>$userid),array('groupids'=>getGroupIds($userid)));
			}

			//更新
			$count_group = aac('group')->findCount('group_user',array(
				'userid'=>$userid,
			));
			aac('user')->update('user_info',array('userid'=>$userid),array('count_group'=>$count_group));

			//计算小组会员数
			$count_user = $new['group']->findCount('group_user',array(
				'groupid'=>$groupid,
			));

			//更新小组成员统计
			$new['group']->update('group',array(
				'groupid'=>$groupid,
			),array(
				'count_user'=>$count_user,
			));
		}else{
			$new['group'] -> update('group_user',array('groupid'=>$groupid,'userid'=>$userid),array('isvo'=>0));
		}
		getJson('操作成功！',1,1);

		break;
		
	//话题推荐
	case "isrecommend":
	
		$js = intval($_GET['js']);
		
		$topicid = intval($_POST['topicid']);
		
		if($TS_USER['isadmin']==1 && $topicid){
		
			$strTopic = $new['group']->find('group_topic',array(
				'topicid'=>$topicid,
			));
			
			if($strTopic['isrecommend']==1){
				$new['group']->update('group_topic',array(
					'topicid'=>$topicid,
				),array(
					'isrecommend'=>0,
				));
				
				getJson('取消推荐成功！',$js);
				
			}
			
			if($strTopic['isrecommend']==0){
				$new['group']->update('group_topic',array(
					'topicid'=>$topicid,
				),array(
					'isrecommend'=>1,
				));
				
				getJson('推荐成功！',$js);
				
			}
			
		
		}else{
		
			getJson('非法操作',$js);
		
		}
		
		break;
	//话题推荐
	case "isdes":

		$js = intval($_GET['js']);

		$articleid = intval($_POST['articleid']);

		if($TS_USER['isadmin']==1 && $articleid){

			$strArticle = aac('article')->find('article',array(
				'articleid'=>$articleid,
			));
			$strGroup =  $new['group']->find('group',array(
				'groupid'=>$strArticle['groupid']
			));

			if($strGroup['articleid']==$articleid){
				$new['group']->update('group',array(
					'groupid'=>$strArticle['groupid']
				),array(
					'articleid'=>0,
				));

				getJson('取消描述成功！',$js,1);

			}

			if($strGroup['articleid']!=$articleid){
				$new['group']->update('group',array(
					'groupid'=>$strArticle['groupid']
				),array(
					'articleid'=>$articleid,
				));

				getJson('设为描述成功！',$js,1);

			}


		}else{

			getJson('非法操作',$js);

		}

		break;
	//统计服务时长
	case 'dutyCount':
		$js = intval($_GET['js']);

  		$groupid = intval($_POST['groupid']);
		$first = intval($_POST['first']);
		$arrGroup = $new['group']->getOneGroup($groupid);

		$thismonth = date('Y-m-01');
		$first = strtotime($thismonth.'-'.$first.'month');

		$begin = date('Y-m-d',$first);
		$last = strtotime(date('Y-m-d',strtotime($begin. '+1 month')));

		$arr = 'begintime>='.$first.' and begintime<'.$last;

		$arrGroupUser = $new['group']->findAll('group_user',array(
			'groupid'=>$groupid,
		));
        $data='<div style="margin-left:10%;"><p>'.$arrGroup['groupname'].date('Y',$first).'年'.date('m',$first).'月值班统计</p><table style="width: 80%;border: 1px solid"><tr><th style="padding:5px;border:1px solid">姓名</th><th style="padding:5px;border:1px solid">次数</th><th style="padding:5px;border:1px solid">时长</th></tr>';
		foreach($arrGroupUser as $key=>$item){
			$arrDuty = $new['group']->findAll('duty',$arr.' and userid='.$item['userid'].' and status=1 and groupid='.$groupid);
			$dutyCount=0;
			$timeCount=0;
			foreach($arrDuty as $v){
				$dutyCount=$dutyCount+1;
				$timeCount=$timeCount+$v['term'];
			}

			$arrUser = aac ( 'user' )->getOneUser ( $item ['userid'] );
			$data = $data.'<tr><td style="padding:5px;border:1px solid">'.$arrUser['username'].'</td><td style="padding:5px;border:1px solid">'.$dutyCount.'</td><td style="padding:5px;border:1px solid">'.$timeCount.'</td></tr>';
		}
		$data = $data.'</table></div>';
		getJson($data,1);

		break;

	//取消馆东身份
	case 'cancel':
		$js = intval($_GET['js']);
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);

		aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('isstock'=>-2,'supporter'=>$TS_USER['userid'],'uptime'=>time()));

		echo 1;
		break;
   //取消候选馆员身份
	case 'cancelvo':
		$js = intval($_GET['js']);
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);

		aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('isvo'=>-2,'supporter'=>$TS_USER['userid'],'uptime'=>time()));

		echo 1;
		break;
}